<?php

namespace App\Admin\Controllers\Task\Assignment;

use App\Models\School\Classes;
use App\Models\Gift\Tag;
use App\Models\Sys\Gift;
use App\Models\Task\Assignment\Confirm;
use App\Models\User\User;
use App\Models\Work;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Illuminate\Support\Facades\DB;
use Symfony\Component\HttpFoundation\Request;

class ConfirmController extends Controller
{
    use ModelForm;

    public $header = '作业龙虎榜';
    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $work_id = \Illuminate\Support\Facades\Request::get('work_id');

        if (!$work_id){
            abort(404);
        }


        return Admin::grid(Confirm::class, function (Grid $grid) use ($work_id) {

            $grid->model()->where('task_id',$work_id)
                ->where('status',1)
                ->orderBy('id','asc');
            $grid->column('学生学号')->display(function (){
                $v = $this->student;
                if ($v){
                    $v = pos($v->toArray());
                    return $v['student_id'];
                }
            });

            $grid->complete_time('提交时间')->display(function ($v){
                return date('Y-m-d H:i:s',$v);
            });

            $grid->user()->telephone('家长手机号');

            $grid->student('学生姓名')->display(function ($v){
                $v = $this->student;
                if ($v){
                    $v = pos($v->toArray());
                    return $v['name_class'];
                }
            });

            $grid->correct_time('处理时间')->display(function ($v){
                if ($v){
                    return date('Y-m-d H:i:s',$v);
                } else {
                    return 'N/A';
                }
            });


            $grid->actions(function (Grid\Displayers\Actions $actions){
               $actions->disableEdit();
            });

            $grid->filter(function (Grid\Filter $filter){
                $filter->disableIdFilter();
                $filter->equal('class_id', '班级id');
                $filter->like('class_name', '班级');
                $filter->between('create_at', '发布时间')->date();
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Work::class, function (Form $form) {

            $form->display('work_id', 'ID');

        });
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header($this->header)
            ->description('Detail')
            ->row($this->detail($id));
    }

    public function detail($id)
    {

    }

}
